package com.neca.auction.pages;

import org.apache.tapestry.annotations.ApplicationState;
import org.apache.tapestry.annotations.Component;
import org.apache.tapestry.annotations.Persist;
import org.apache.tapestry.corelib.components.Form;
import org.apache.tapestry.ioc.Messages;
import org.apache.tapestry.ioc.annotations.Inject;

import com.neca.auction.dbb.DBBroker;
import com.neca.auction.model.CurrentUser;
import com.neca.auction.model.Offer;
import com.neca.auction.model.Product;
import com.neca.auction.model.User;

public class ProductView {
	@Persist
	private Product product;
	private double myHighestOffer;
	@Component
	private Form plForm;
	@Inject
	private Messages messages;
	@ApplicationState
	private CurrentUser currentUser;

	public Product getProduct() {
		return product;
	}

	public void setProduct(Product product) {
		this.product = product;
	}
	
	void onActivate(int productID){
		DBBroker dbbroker=DBBroker.getDbbroker();
		product=dbbroker.getProduct(productID);
	}
	
	public double getHighestOffer() {
		
	
		DBBroker dbbroker=DBBroker.getDbbroker();
		double price= dbbroker.getHighestOffer(product);
		if(product.getPrice()<price){
			return price;
		}
		else{
			return product.getPrice();
		}
		
	}
	
	public double getMyHighestOffer() {
		return myHighestOffer;
	}

	public void setMyHighestOffer(double myHighestOffer) {
		this.myHighestOffer = myHighestOffer;
	}
	
	
	void onSubmitFromPlForm(){
		System.out.println("Form was submitted!!!");
		
		
		
	}
	void onValidate(){
		
		
	}
	
	void onSuccess(){
		
		DBBroker dbbroker=DBBroker.getDbbroker();
		if(myHighestOffer>product.getPrice()){
		if(myHighestOffer<dbbroker.getHighestOffer(product)){
			//Product p=dbbroker.getProduct(productID);
			//category = p.getCategory();
			setMyHighestOffer(0);
			//licitationForm.recordError(messages.get("passwor-match"));
			plForm.clearErrors();
			plForm.recordError(messages.get("err-price"));
			return; //AllProducts.class;
			
		}
		}else{
			//licitationForm.recordError(messages.get("passwor-match"));
			//Product p=dbbroker.getProduct(productID);
			//category = p.getCategory();
			plForm.clearErrors();
			plForm.recordError(messages.get("err-price"));
			return; //AllProducts.class;
		}
		
		User u = new User("","",currentUser.getUserName(),"","","",0);
		User user=dbbroker.getUserByUserName(u);
		dbbroker.addOffer(new Offer(product.getProductID(),user.getUserID() , myHighestOffer));
		//Product p=dbbroker.getProduct(productID);
		//category = p.getCategory();
		//System.out.println("ubaceno u bazu!!!!!!!!!!!!!");
		plForm.clearErrors();
		//return AllProducts.class;
		
		/*System.out.println(product.getName());
		DBBroker dbbroker=new DBBroker();
		User u = new User("","",currentUser.getUserName(),"","","",0);
		User user=dbbroker.getUserByUserName(u);
		dbbroker.addOffer(new Offer(productID,user.getUserID() , myHighestOffer));
		
		return AllProducts.class;*/
	}
	
	void onFailure(){
		System.out.println("onFailure.");
		
	}

}
